
<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list v-model="loading"
              :finished="finished"
              finished-text="没有更多了"
              @load="onLoad">
      <van-cell v-for="(item,index) in list"
                :key="index">
        <!-- 左侧头像区域 -->
        <template #icon>
          <van-image round
                     width="30"
                     height="30"
                     style="margin-right: 10px"
                     :src="item.aut_photo" />
        </template>
        <!-- 评论标题区域 -->
        <template #title>
          <span style="color: #466b9d">{{item.aut_name}}</span>
        </template>
        <!-- 评论详情区域 -->
        <template #label>
          <p style="color: #363636">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px">{{relativeTime(item.pubdate)}}</span>
          </p>
        </template>
        <!--右侧侧点赞评论区域 -->
        <template #right-icon>
          <van-icon :name="item.is_liking ? 'like':'like-o'"
                    @click="toggleLike(item)" />
        </template>
      </van-cell>
    </van-list>

    <!-- 发布评论区域 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable
                 placeholder="请输入评论内容"
                 v-model=value>
        <template #button>
          <van-button size="mini"
                      @click="fabu">发布</van-button>
        </template>
      </van-field>
    </van-cell-group>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import { getCommets, addComment, addCommentLike, deleteCommentLike } from '@/api/commet.js'
export default {
  name: 'ArticleComment',
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null, // 最后一个评论的id
      value: '' // 评论内容
    }
  },
  methods: {
    onLoad () {
      // 获取当前文章的id
      const id = this.$route.query.artID
      getCommets(id, this.offset).then(res => {
        const arr = res.data.data.results
        console.log(arr)
        // 1.拼接数据
        this.list = [...this.list, ...arr]
        // 2.手动将loading设置为false
        this.loading = false
        // 如果获取不到了，调用finished=true，显示没有更多了
        // 3.判断所有数据加载完毕 停止上拉加载
        if (arr.length === 0) {
          this.finished = true
        }
        // 4.手动设置offset为当前newList中最后一项的id值
        this.offset = res.data.data.last_id
      })
    },
    // 发布评论
    fabu () {
      const artId = this.$route.query.artID
      addComment(artId, this.value).then(res => {
        const arr = res.data.data.new_obj
        this.list = [arr, ...this.list]
        this.value = ''
        this.$toast.success('发布成功！')
      }).catch(() => {
        this.$toast.success('发布失败！')
      })
    },
    // 是否喜欢
    toggleLike (comment) {
      // 如果是喜欢的话，点击会变为不稀罕，所以要调不稀罕的接口
      if (comment.is_liking) {
        deleteCommentLike(comment.com_id.toString()).then(res => {
          console.log(res)
          this.$toast.success('不喜欢')
        })
      } else {
        addCommentLike(comment.com_id.toString()).then(res => {
          this.$toast.success('喜欢')
        })
      }
      // 点击切换状态
      comment.is_liking = !comment.is_liking
    }

  }

}
</script>

<style scoped lang='less'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
